#![allow(non_camel_case_types, non_snake_case)]
use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::{ffi, privs::*};
use crate::prelude::*;
impl_handle! { HEVENT;
}
impl kernel_Hevent for HEVENT {}
pub trait kernel_Hevent: Handle {
#[must_use]
fn CreateEvent(
security_attributes: Option<&mut SECURITY_ATTRIBUTES>,
manual_reset: bool,
initial_state: bool,
name: Option<&str>,
) -> SysResult<CloseHandleGuard<HEVENT>>
{
unsafe {
ptr_to_sysresult_handle(
ffi::CreateEventW(
security_attributes.map_or(std::ptr::null_mut(), |sa| sa as *const _ as _),
manual_reset as _,
initial_state as _,
WString::from_opt_str(name).as_ptr(),
)
).map(|h| CloseHandleGuard::new(h))
}
}
#[must_use]
fn CreateEventEx(
security_attributes: Option<&mut SECURITY_ATTRIBUTES>,
name: Option<&str>,
flags: co::CREATE_EVENT,
desired_access: co::EVENT_RIGHTS,
) -> SysResult<CloseHandleGuard<HEVENT>>
{
unsafe {
ptr_to_sysresult_handle(
ffi::CreateEventExW(
security_attributes.map_or(std::ptr::null_mut(), |sa| sa as *const _ as _),
WString::from_opt_str(name).as_ptr(),
flags.raw(),
desired_access.raw(),
)
).map(|h| CloseHandleGuard::new(h))
}
}
#[must_use]
fn OpenEvent(&self,
desired_access: co::EVENT_RIGHTS,
inherit_handle: bool,
name: &str,
) -> SysResult<CloseHandleGuard<HEVENT>>
{
unsafe {
ptr_to_sysresult_handle(
ffi::OpenEventW(
desired_access.raw(),
inherit_handle as _,
WString::from_str(name).as_ptr(),
)
).map(|h| CloseHandleGuard::new(h))
}
}
fn PulseEvent(&self) -> SysResult<()> {
bool_to_sysresult(unsafe { ffi::PulseEvent(self.ptr()) })
}
fn ResetEvent(&self) -> SysResult<()> {
bool_to_sysresult(unsafe { ffi::ResetEvent(self.ptr()) })
}
fn SetEvent(&self) -> SysResult<()> {
bool_to_sysresult(unsafe { ffi::SetEvent(self.ptr()) })
}
fn WaitForSingleObject(&self,
milliseconds: Option<u32>,
) -> SysResult<co::WAIT>
{
match unsafe {
co::WAIT::from_raw(
ffi::WaitForSingleObject(
self.ptr(),
milliseconds.unwrap_or(INFINITE),
),
)
} {
co::WAIT::FAILED => Err(GetLastError()),
wait => Ok(wait),
}
}
}